﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Panel_Main : BasePanel
{
    private bool cancelTime;
    private CanvasGroup canvas;
    private LineRenderer line;
    /// <summary>
    /// 角色中心到y轴顶端的偏差值
    /// </summary>
    private float offset_Y = 0.5f;

    public GameObject shadow;
    public Text textMoney;
    [Header("画线端点")]
    public GameObject point;
    public GameObject character;

    private void Awake()
    {
        canvas = GetComponent<CanvasGroup>();
        line = transform.GetComponent<LineRenderer>();
        line.startWidth = 0.1f;
    }

    private void Start()
    {
        ShowTextMoney();
        line.SetPosition(0, point.transform.position);

#if !UNITY_EDITOR
        //Debug.Log("knock");
         User.instance.DataRead();
#endif

    }

    private void Update()
    {
        DrawnLine();
        QuitGame();
        EasterEgg();
    }

    public override void OnEnter()
    {
        base.OnEnter();
        gameObject.SetActive(true);
    }

    public override void OnExit()
    {
        base.OnExit();
        gameObject.SetActive(false);

    }

    public override void OnPause()
    {
        base.OnPause();
        canvas.blocksRaycasts = false;
        line.enabled = false;
        shadow.gameObject.SetActive(true);
    }

    public override void OnResume()
    { 
        base.OnResume();
        canvas.blocksRaycasts = true;
        line.enabled = true; 
        ShowTextMoney();
        shadow.gameObject.SetActive(false);
    }

    public void Button_UIChange(string panelTypeString)
    {
        UIPanelType panelType = (UIPanelType)System.Enum.Parse(typeof(UIPanelType), panelTypeString);
        UIManager.Instance.PushPanel(panelType);
        
    }

    /// <summary>
    /// 画线函数
    /// </summary>
    public void DrawnLine()
    {
        if (line.GetPosition(0) != point.transform.position)
        {
            line.SetPosition(0, point.transform.position);
        }
        line.SetPosition(1, character.transform.position + character.transform.up * offset_Y);
    }

    /// <summary>
    /// 开始游戏
    /// </summary>
    public void Button_BeginGame()
    {
        GameControl.instance.GameBegin();
    }

    public void WatchForMoney()
    {
        /******广告位*******/
        User.instance.moneyBool = true;
        GoogleAdMobController.instance.ShowRewardAd();

        //User.instance.ChangeMoney(1000);
    }

    /// <summary>
    /// 双击返回 退出游戏
    /// </summary>
    public void QuitGame()
    {
        if (Input.GetButtonDown("Cancel") && line.enabled)
        {
            if (cancelTime)
            {
                Application.Quit();
                //Debug.Log("twice");
            }
            else
            {
                cancelTime = true;
                StartCoroutine(Timer());
                //Debug.Log("once");
            }
        }
    }
    public IEnumerator Timer()
    {
        cancelTime = true;
        yield return new WaitForSeconds(1f);
        cancelTime = false;
    }

    public void EasterEgg()
    {
        if (Input.GetKey(KeyCode.R) && Input.GetKeyDown(KeyCode.E))
        {
            //PlayerPrefs.SetInt("money", 0);
            //PlayerPrefs.SetInt("CurLevel", 0);
            //PlayerPrefs.SetInt("DateOfSign", 0);
            //PlayerPrefs.SetInt("DaysOfSign", 0);
            User.instance.CurLevel = 0;
            PlayerPrefs.DeleteAll();

            Debug.Log("数据还原成功");
        }

    }

    public void ShowTextMoney()
    {
        textMoney.text = User.instance.Money.ToString();
    }




}
